#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#Author: alvayang <alvayang@tabex.org>
#Last Change: 
#Description: 

def import_modules(modules):
    urls = {}
    g = globals()
    from imp import load_source
    def import_module(name, file):
        m = load_source(name, file)
        g[name] = m
        v = dir(m)
        h, u = 'handler' in v, 'urls' in v
        if h or u:
            if not (h and u):
                raise ImportError('module %s doesn\'t have matched handler and urls' % name)
            urls[m.urls] = m.handler()
    for (name, file) in modules:
        if name not in g:
            import_module(name, file)
    return urls
    

def error(msg):
    print >> sys.stderr, str(msg)
